package com.talkweb.ncfw.web.action;

import java.io.IOException;
import java.util.List;
import java.util.Map;

import org.springframework.util.CollectionUtils;

import com.talkweb.ncfw.component.dict.utils.DictUtils;
import com.talkweb.ncfw.component.log.entity.SysOperateLog;
import com.talkweb.ncfw.component.log.service.ILogService;
import com.talkweb.ncfw.data.IDataMap;
import com.talkweb.ncfw.data.Page;
import com.talkweb.ncfw.exceptions.BizBaseException;
import com.talkweb.ncfw.exceptions.ViewBaseException;
import com.talkweb.ncfw.report.poi.CsvBean;
import com.talkweb.ncfw.report.poi.ExportUtils;
import com.talkweb.ncfw.report.poi.ReportBeanUtils;
import com.talkweb.ncfw.web.struts2.ExtGridBaseAction;
import com.talkweb.ncfw.web.utils.HttpServletUtils;

/**
 * <p>文件名称: LogAction.java</p>
 * <p>文件描述: 日志Action</p>
 * <p>版权所有: 版权所有(C)2010</p>
 * <p>公　　司: 拓维信息系统股份有限公司</p>
 * <p>内容摘要: 简要描述本文件的内容，包括主要模块、函数及能的说明</p>
 * <p>其他说明: 其它内容的说明</p>
 * <p>完成日期: 2011-4-6</p>
 * <p>修改记录1:</p>
 * <pre>
 *    修改日期:
 *    修 改 人:
 *    修改内容:
 * </pre>
 * <p>修改记录2：…</p>
 * @author  Wuqingming
 */
public class LogAction extends ExtGridBaseAction {
	
	/**
	 * 
	 */
	private static final long serialVersionUID = -3372289237271706687L;
	
	//表头
	private String[] columnHeadArray;
	//列宽
	@SuppressWarnings("unused")
	private String[] columnWidthArray;
	//索引
	private String[] dataIndexArray;

	/** 日志服务 */
	private ILogService logService;	
	
	public ILogService getLogService() {
		return logService;
	}

	public void setLogService(ILogService logService) {
		this.logService = logService;
	}

	/**
	 * 
	 * @author：Wuqingming 	        
	 * @date：2011-4-12
	 * @Description：列出系统操作日志列表
	 * @return
	 */
	public String listSysOperateLog () throws ViewBaseException {
		Page<SysOperateLog> page = this.getPage();
		IDataMap<String, Object> condition = this.getParameterMap(); 
		try {
			page = this.getLogService().findSysOperateLog(page, condition);
		} catch (BizBaseException e) {
			throw new ViewBaseException(e.getMessage(), e);
		}
		HttpServletUtils.outJson(this.getResponse(), page);
		return null;
	}
	
	/**
	 * 
	 * @author：Wuqingming
	 * @date：2011-4-12
	 * @Description：构造导出系统操作日志表头
	 */
	private void buildExportSysOperateLogHead () {
		dataIndexArray 		= new String[] {"operateType", "beginTime", "operateResult", "module", 
				"className", "methodName", "methodDescription", "methodParameters", "operator", "remark"};
		columnHeadArray 	= new String[] {"操作类型", "操作时间", "结果", "所属模块", 
				"类名", "方法名", "方法描述", "执行参数", "操作者", "备注"};
		columnWidthArray 	= new String[] {"80, 130", "60, 130", "140, 160", "130", "220, 120", "280"};
	}

	/**
	 * 
	 * @author：Wuqingming 	        
	 * @date：2011-4-12
	 * @Description：导出系统操作日志
	 * @return
	 */
	public String exportSysOperateLog () throws ViewBaseException {
		Map<String, Object> condition = null;
		Page<SysOperateLog> page = new Page<SysOperateLog>();
		page.setIfPage(false);
		page.setAutoCount(false);
		try {
			page = this.getLogService().findSysOperateLog(page, condition);
		} catch (BizBaseException e1) {
			throw new ViewBaseException(e1.getMessage(), e1);
		}
		List<SysOperateLog> sysOperateLogList = page.getResult();
		
		/** 字典转化处理 */
		if (!CollectionUtils.isEmpty(sysOperateLogList)) {
			for (SysOperateLog sysOperateLog : sysOperateLogList) {
				sysOperateLog.setOperateType(DictUtils.getDictEntryName("T_PUB_LOG_OPERATE_TYPE", sysOperateLog.getOperateType()));
				sysOperateLog.setOperateResult(DictUtils.getDictEntryName("T_PUB_LOG_OPERATE_RESULT", sysOperateLog.getOperateResult()));
				sysOperateLog.setModule(DictUtils.getDictEntryName("T_PUB_MODULE", sysOperateLog.getModule()));
			}
		}
		buildExportSysOperateLogHead();
		CsvBean<SysOperateLog> csvBean = ReportBeanUtils.createCsvBean(sysOperateLogList, dataIndexArray, columnHeadArray, "系统操作日志");
		try {
			ExportUtils.exportCsv(csvBean, this.getRequest(), this.getResponse());
		} catch (IOException e) {
			throw new ViewBaseException("导出系统操作日志时发生IO异常", e);
		}
		return null;
	}

	public String getSysOperateLog () throws ViewBaseException {
		String logId = this.getParameter("logId");
		SysOperateLog sysOperateLog = null;
		try {
			sysOperateLog = this.getLogService().getSysOperateLog(logId);
		} catch (BizBaseException e) {
			throw new ViewBaseException(e.getMessage(), e);
		}
		HttpServletUtils.outJson(this.getResponse(), sysOperateLog);
		return null;
	}
	
}

